package com.zheng.api.server.service.mq.um;

import com.alibaba.fastjson.JSONObject;
import com.zheng.api.common.constant.ApiConstant;
import com.zheng.api.common.constant.ApiResult;
import com.zheng.common.constant.MqConstant;
import com.zheng.common.util.DateUtil;
import com.zheng.common.util.JsonUtil;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

import javax.jms.Topic;
import java.util.Date;

/**
 * @Description: 伞机通知MQ统一处理
 * @author lvb
 * @date 2017-10-31
 * @version V1.0
 * @Copyright:
 */
@Service
public class Mq4UmbrellaMachineNotify extends Mq4UmbrellaMachine {

    private static final Logger LOGGER = LoggerFactory.getLogger(Mq4UmbrellaMachineNotify.class);
    private Topic umbrellaMachineTopic =  new ActiveMQTopic(MqConstant.ZHENG_CLIENT_TOPIC_UMBRELLA_MACHINE_NAME);


    public void send(String msg,String clientId) {
        String logPrefix = "【后台向伞机发送指令通知】";
        Date beginTime = DateUtil.getNowDate();
        try {
            super.send(umbrellaMachineTopic, msg, 20, clientId);
        }catch (Exception e) {
            LOGGER.error("{}发送消息:msg={}", logPrefix, msg,e);
        }finally{
            Date endTime = DateUtil.getNowDate();
            super.recordUmbrellaHardwareMqttcommandLog(beginTime,msg,endTime,null);
        }
    }

    //@JmsListener(destination = MqConstant.ZHENG_SERVER_TOPIC_UMBRELLA_MACHINE_NAME,containerFactory="jmsTopicListenerContainer")
    //public void receive(String msg) {
    public void receive(String msg) {
        String logPrefix = "【后台接收伞机指令通知】";
        //String msg = new String(bytes);
        LOGGER.info("{}接收消息:msg={}", logPrefix, msg);
        ApiResult result = null;
        Date beginTime = DateUtil.getNowDate();
        try {
            JSONObject jsonMsg = JsonUtil.getJSONObjectFromJson(msg);
            String command = JsonUtil.getString(jsonMsg,"command");
            String machineCode = JsonUtil.getString(jsonMsg,"machineCode");
            int code = JsonUtil.getInteger(jsonMsg,"code");
            String message = JsonUtil.getString(jsonMsg,"message");
            JSONObject recvData = JsonUtil.getJSONObject(jsonMsg,"data");

            if(StringUtils.isEmpty(command)){
                LOGGER.error("{}接收消息:msg={}", "参数不正确，command为空", msg);
            }else{
                switch (command) {
                    case ApiConstant.UMBRELLA_MACHINE_COMMAND_20002:
                        //设备状态上报【20002】
                        //machineCommandInterface.doCommand20002();
                        result = super.baseMachineBusinessCommandFactory(machineCode).doCommand20002(command,machineCode, code, message, recvData);
                        break;
                    case ApiConstant.UMBRELLA_MACHINE_COMMAND_20004:
                        //通信连接建立失败通知【20004】,见ApiCommonService.notify
                        break;
                    case ApiConstant.UMBRELLA_MACHINE_COMMAND_20101:
                        //扫码借伞-雨伞识别结果上报【20101】
                        result = super.baseMachineBusinessCommandFactory(machineCode).doCommand20101(command,machineCode, code, message, recvData);
                        break;
                    case ApiConstant.UMBRELLA_MACHINE_COMMAND_20102:
                        //扫码借伞-出伞结果上报【20102】
                        result = super.baseMachineBusinessCommandFactory(machineCode).doCommand20102(command,machineCode, code, message, recvData);
                        break;
                    case ApiConstant.UMBRELLA_MACHINE_COMMAND_20105:
                        //扫码还伞-还伞结果上报【20105】
                        result = super.baseMachineBusinessCommandFactory(machineCode).doCommand20105(command,machineCode, code, message, recvData);
                        break;
                    case ApiConstant.UMBRELLA_MACHINE_COMMAND_20106:
                        //物理还伞(二次交互模式)-还伞识别上报【20106】
                        result = super.baseMachineBusinessCommandFactory(machineCode).doCommand20106(command,machineCode, code, message, recvData);
                        break;
                    case ApiConstant.UMBRELLA_MACHINE_COMMAND_20107:
                        //物理还伞(二次交互模式)-还伞结果上报【20107】
                        result = super.baseMachineBusinessCommandFactory(machineCode).doCommand20107(command,machineCode, code, message, recvData);
                        break;
                    case ApiConstant.UMBRELLA_MACHINE_COMMAND_20108:
                        //物理还伞(一次交互模式）【20108】
                        result = super.baseMachineBusinessCommandFactory(machineCode).doCommand20108(command,machineCode, code, message, recvData);
                        break;
                    case ApiConstant.UMBRELLA_MACHINE_COMMAND_20110:
                        //雨伞管理-进伞结果上报【20110】
                        result = super.baseMachineBusinessCommandFactory(machineCode).doCommand20110(command,machineCode, code, message, recvData);
                        break;
                    case ApiConstant.UMBRELLA_MACHINE_COMMAND_20112:
                        //雨伞管理-出伞结果上报【20112】
                        result = super.baseMachineBusinessCommandFactory(machineCode).doCommand20112(command,machineCode, code, message, recvData);
                        break;
                    case ApiConstant.UMBRELLA_MACHINE_COMMAND_20998:
                        //心跳
                        result = super.baseMachineBusinessCommandFactory(machineCode).doCommand20998(command,machineCode, code, message, recvData);
                        break;
                    default:
                        LOGGER.error("{}接收消息:msg={}", logPrefix, msg,"指令不正确");
                        break;
                }
             }
        }catch (Exception e) {
            LOGGER.error("{}接收消息:msg={}", logPrefix, msg,e);
        }finally{
            Date endTime = DateUtil.getNowDate();
            super.recordUmbrellaHardwareMqttcommandLog(beginTime,msg,endTime,JsonUtil.object2Json(result));
        }
        LOGGER.info("{}处理消息完成:msg={}", logPrefix, msg);
    }
}
